<?php
/**
 * The customer class.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-10-30
 */
require_once 'database.php';
require_once 'table.php';

class customer extends table {
	var $name;
	var $phone_num;
	var $email;
	var $address;

	/**
	 * The constructor of the customer class.
	 *
	 * @param i		The ID of this customer.
	 * @return		The newly initialized customer object.
	 */
	function __construct($i) {
		parent::__construct ($i,'Customers');
		$this->name 		= "";
		$this->phone_num 	= -1;
		$this->email 		= "";
		$this->address 		= "";
	}

	/**
	 * Initialize this object from data.
	 *
	 * @param nm	The name of this customer.
	 * @param phn	The phone number of this customer.
	 * @param em	The email address for this customer.
	 * @param addr	The address of this customer.
	 * @return		None.
	 */
	function init_from_data($nm,$phn,$em,$addr) {
		$this->name 		= $nm;
		$this->phone_num 	= $phn;
		$this->email 		= $em;
		$this->address 		= $addr;
	}

	/**
	 * Initialize this employee object from form data.
	 *
	 * @param post	The _POST variable from a form.
	 * @return		Whether or not the initialization was successful.
	 */
	function init_from_post($post) {
		if (!(isset ($post['name'])
			 && isset ($post['phone_num'])
			 && isset ($post['email'])
			 && isset ($post['address'])))
			return FALSE;
		$this->init_from_data (
			$post['name'], 
			$post['phone_num'], 
			$post['email'], 
			$post['address']
        );
		return TRUE;
	}

	/**
	 * Update the address of this employee.
	 *
	 * @param db			A database with a connection.
	 * @param new_address	The new address.
	 */
	function update_address ($db, $new_address) {
		$this->address = $new_address;
		$this->update_database_by_id ($db);
	}

	/**
	 * Update the email of this employee.
	 *
	 * @param db			A database with a connection.
	 * @param new_email		The new email address.
	 */
	function update_email ($db, $new_email) {
		$this->email = $new_email;
		$this->update_database_by_id ($db);
	}

	/**
	 * Displays a form to select a customer from the database.
	 *
	 * @param action	The action to perform on submission.
	 */
	static function select ($action) {
		$db = new database ();
		$customers = $db->query ("SELECT id, name FROM Customers");
		echo "<form name=\"customer\" action=\"" . $action . "\" method=\"post\">\n";
		echo "<select name=\"name\">\n";
		while ($row = $customers->fetch_assoc ()) {
			$id = $row['id']; $name = $row['name'];
			echo "<option value='$id'>$name</option>\n";
		}
		echo "</select><br>";
		echo "<input type=\"submit\" value=\"Go\"><br>\n";
		echo "</form><br>\n";
	}

}

?>